package com.yima.baseproj.module.sys.clazz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.aop.log.Logging;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.config.utils.ResultUtil;
import com.yima.baseproj.module.config.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "班级管理")
@RequestMapping("/api/sys/class")
@RestController
public class ClassController {

    @Autowired
    private ClassService classService;

    @Logging(tags = "班级管理",value = "查询分页")
    @PreAuthorize("hasAuthority('sys:class:findAllByPage')")
    @ApiOperation("查询分页")
    @GetMapping("/findAllByPage")
    public Object findAllByPage(PageVo page, ClassEntity searchModel) {
        Page<Map> pageData = classService.findAllByPage(PageUtil.initMpPage(page),searchModel);
        return pageData;
    }

    @Logging(tags = "班级管理",value = "添加")
    @PreAuthorize("hasAuthority('sys:class:add')")
    @ApiOperation("添加")
    @PostMapping("/add")
    public void saveUser(@RequestBody @Validated ClassEntity customAdmin) {

        classService.add(customAdmin);
    }

    @ApiOperation("根据id查询班级信息")
    @GetMapping("/findById/{id}")
    public ClassEntity findById(@PathVariable int id){
        ClassEntity classEntity = classService.findById(id);
        return classEntity;
    }

    @Logging(tags = "班级管理",value = "修改")
    @PreAuthorize("hasAuthority('sys:class:edit')")
    @ApiOperation("修改")
    @PostMapping("/edit")
    public void editcl(@RequestBody @Validated ClassEntity customAdmin) {
        classService.edit(customAdmin);
    }

    @Logging(tags = "班级管理",value = "删除")
    @PreAuthorize("hasAuthority('sys:class:delByIds')")
    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delByIds(@PathVariable int ids) {

        classService.delByIds(ids);
    }

    @PreAuthorize("hasAuthority('sys:class:findAllByList')")
    @ApiOperation("查询列表")
    @GetMapping("/findAllByList")
    public ResultVo getClass(ClassEntity classEntity) {
        List<Map> classList = classService.findAllByList(classEntity);
        return ResultUtil.success(classList);
    }


    @ApiOperation("根据幼儿园id查询班级列表")
    @GetMapping("/findAllByKgId/{kgId}")
    public ResultVo findAllByKgId(@PathVariable int kgId) {
        List<ClassEntity> classList = classService.findAllByKgId(kgId);
        return ResultUtil.success(classList);
    }

}
